#include <vector>
#include <iostream>
#include <algorithm>
#include <utility>s
using namespace std;

class Solution 
{
public:
	vector<int> spiralOrder(vector<vector<int> > & mx)
	{
		// Start typing your C/C++ solution below
		// DO NOT write int main() function
		size_t m = mx.size();
		if(m<=0)
			return vector<int>();

		size_t n = mx[0].size();
		if(n<=0)
			return vector<int>();

		vector<int> result(m*n);


		int nsteps[4] = {n-1,m-1,n-1,m-2};
		int steps[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
		size_t curIdx = 0, x = 0, y = 0;
		do
		{
			result[curIdx++] = mx[x][y];
			for(int i=0;i<4;i++)
			{
				if(curIdx>=result.size())
					return result;
				for(int j=0;j<nsteps[i];j++,curIdx++)
				{
					x += steps[i][0];
					y += steps[i][1];
					result[curIdx] = mx[x][y];					
				}
				nsteps[i] -= 2;
			}
			y++;
		}while(curIdx<result.size());

		return result;
	}
};

int main()
{
	int m,n;
	Solution s;
	while(cin>>m)
	{
		if(m==0)
			break;
		cin>>n;
		vector<vector<int> > matrix(m);
		for(int i=0;i<m;i++)
		{
			matrix[i] = vector<int>(n);
			for(int j=0;j<n;j++)
				cin>>matrix[i][j];
		}

		vector<int> r = s.spiralOrder(matrix);
		for(size_t i=0;i<r.size();i++)
			cout<<r[i]<<" ";
		cout<<endl;
	}
}